package mx.com.matersys.vacs.daos;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import mx.com.matersys.vacs.domain.Paciente;

import org.springframework.transaction.annotation.Transactional;


privileged aspect PacienteDao {
declare @type: Paciente: @Entity;
    
    @PersistenceContext
    transient EntityManager Paciente.entityManager;
    
    @Transactional
    public void Paciente.persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }
    
    @Transactional
    public void Paciente.remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Paciente attached = Paciente.findPaciente(this.idpaciente);
            this.entityManager.remove(attached);
        }
    }
    
    @Transactional
    public void Paciente.flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }
    
    @Transactional
    public void Paciente.clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }
    
    @Transactional
    public Paciente Paciente.merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Paciente merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
    
    public static final EntityManager Paciente.entityManager() {
        EntityManager em = new Paciente().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }
    
    public static long Paciente.countPacientes() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Paciente o", Long.class).getSingleResult();
    }
    
    public static List<Paciente> Paciente.findAllPacientes() {
        return entityManager().createQuery("SELECT o FROM Paciente o", Paciente.class).getResultList();
    }
    
    public static Paciente Paciente.findPaciente(Integer idpaciente) {
        if (idpaciente == null) return null;
        return entityManager().find(Paciente.class, idpaciente);
    }
    
    public static List<Paciente> Paciente.findPacienteEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Paciente o", Paciente.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }
    
    public static List<Paciente> Paciente.findPacienteLikeLastName(String apPaterno) {
        
		return entityManager().createQuery("SELECT o FROM Paciente AS o where o.apPaterno like :apPaterno",
				Paciente.class).setParameter("apPaterno", apPaterno).getResultList();
		// .setParameter("apPaterno",
								// apPaterno).getResultList();
    
    	//entityManager().;
    	
    	/*
        TypedQuery<Paciente> q = em.createQuery("SELECT o FROM Rsvp AS o WHERE o.code = :code", Paciente.class).setParameter(arg0, arg1);
        q.setParameter("code", code);
    	*/
    	//return entityManager().createQuery("SELECT o FROM Paciente o", Paciente.class).setFirstResult(1).setMaxResults(2).getResultList();
		
    } 
}
